package cn.com.open.openqti;

import cn.com.open.qti.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lucky on 2017/6/30.
 */
public class OpenTestPart {
    /**
     * ID
     */
    private String identifier;
    /**
     * 大题集合
     */
    private List<OpenAssessmentSection> assessmentSectionCollection;
    /**
     * 试题Session控制
     */
    private ItemSessionControlType itemSessionControl;
    /**
     * 时间限制
     */
    private TimeLimitsType timeLimits;
    /**
     * 答题说明内容
     */
    private OpenPrompt prompt;
    /**
     * 导航模式
     */
    private NavigationModeType navigationMode;
    /**
     * 末尾的答题说明内容
     */
    private OpenPrompt endPrompt;

    /**
     * Gets identifier.
     *
     * @return the identifier
     */
    public String getIdentifier() {
        return identifier;
    }

    /**
     * Sets identifier.
     *
     * @param identifier the identifier
     */
    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    /**
     * Gets assessment section collection.
     *
     * @return the assessment section collection
     */
    public List<OpenAssessmentSection> getAssessmentSectionCollection() {
        if (this.assessmentSectionCollection == null) {
            this.assessmentSectionCollection = new ArrayList<OpenAssessmentSection>();
        }
        return assessmentSectionCollection;
    }

    /**
     * Sets assessment section collection.
     *
     * @param assessmentSectionCollection the assessment section collection
     */
    public void setAssessmentSectionCollection(List<OpenAssessmentSection> assessmentSectionCollection) {
        if (this.assessmentSectionCollection == null) {
            this.assessmentSectionCollection = new ArrayList<OpenAssessmentSection>();
        }
        this.assessmentSectionCollection = assessmentSectionCollection;
    }

    /**
     * Gets item session control.
     *
     * @return the item session control
     */
    public ItemSessionControlType getItemSessionControl() {
        return itemSessionControl;
    }

    /**
     * Sets item session control.
     *
     * @param itemSessionControl the item session control
     */
    public void setItemSessionControl(ItemSessionControlType itemSessionControl) {
        this.itemSessionControl = itemSessionControl;
    }

    /**
     * Gets time limits.
     *
     * @return the time limits
     */
    public TimeLimitsType getTimeLimits() {
        return timeLimits;
    }

    /**
     * Sets time limits.
     *
     * @param timeLimits the time limits
     */
    public void setTimeLimits(TimeLimitsType timeLimits) {
        this.timeLimits = timeLimits;
    }

    /**
     * Gets prompt.
     *
     * @return the prompt
     */
    public OpenPrompt getPrompt() {
        return prompt;
    }

    /**
     * Sets prompt.
     *
     * @param prompt the prompt
     */
    public void setPrompt(OpenPrompt prompt) {
        this.prompt = prompt;
    }

    /**
     * Gets navigation mode.
     *
     * @return the navigation mode
     */
    public NavigationModeType getNavigationMode() {
        return navigationMode;
    }

    /**
     * Sets navigation mode.
     *
     * @param navigationMode the navigation mode
     */
    public void setNavigationMode(NavigationModeType navigationMode) {
        this.navigationMode = navigationMode;
    }

    /**
     * Gets end prompt.
     *
     * @return the end prompt
     */
    public OpenPrompt getEndPrompt() {
        return endPrompt;
    }

    /**
     * Sets end prompt.
     *
     * @param endPrompt the end prompt
     */
    public void setEndPrompt(OpenPrompt endPrompt) {
        this.endPrompt = endPrompt;
    }

    /**
     * 部分（Part）构造函数
     */
    public OpenTestPart() {
    }

    /**
     * 部分（Part）构造函数
     *
     * @param identifier                  ID
     * @param prompt                      答题说明内容
     * @param navigationMode              导航模式
     * @param assessmentSectionCollection 大题集合
     * @param itemSessionControl          试题Session控制
     * @param timeLimits                  时间限制
     * @param endPrompt                   末尾的答题说明
     */
    public OpenTestPart(String identifier, OpenPrompt prompt, NavigationModeType navigationMode, List<OpenAssessmentSection> assessmentSectionCollection,
                        ItemSessionControlType itemSessionControl, TimeLimitsType timeLimits, OpenPrompt endPrompt) {
        setIdentifier(identifier);
        setPrompt(prompt);
        setNavigationMode(navigationMode);

        setTimeLimits(timeLimits);
        setItemSessionControl(itemSessionControl);
        setAssessmentSectionCollection(assessmentSectionCollection);
        setEndPrompt(endPrompt);
    }

    /**
     * 部分（Part）构造函数
     *
     * @param identifier                  ID
     * @param assessmentSectionCollection 大题集合
     */
    private OpenTestPart(String identifier, List<OpenAssessmentSection> assessmentSectionCollection) {
        this(identifier, null, NavigationModeType.NONLINEAR, assessmentSectionCollection, null, null, null);
    }

    /**
     * 部分（Part）构造函数(没有时间限制)
     *
     * @param identifier                  ID
     * @param prompt                      答题说明内容
     * @param navigationMode              导航模式
     * @param assessmentSectionCollection 大题集合
     */
    public OpenTestPart(String identifier, OpenPrompt prompt, NavigationModeType navigationMode, List<OpenAssessmentSection> assessmentSectionCollection) {
        this(identifier, prompt, navigationMode, assessmentSectionCollection, null, null, null);
    }

    /**
     * 部分（Part）构造函数(含时间限制,含结束答题说明)
     *
     * @param identifier                  ID
     * @param prompt                      答题说明内容
     * @param navigationMode              导航模式
     * @param assessmentSectionCollection 大题集合
     * @param maxTime                     最大时间限制
     * @param endPrompt                   末尾答题说明
     */
    public OpenTestPart(String identifier, OpenPrompt prompt, NavigationModeType navigationMode, List<OpenAssessmentSection> assessmentSectionCollection,
                        int maxTime, OpenPrompt endPrompt) {
        setIdentifier(identifier);
        setPrompt(prompt);
        setNavigationMode(navigationMode);

        TimeLimitsType timeLimits = new TimeLimitsType();
        timeLimits.setMaxTime(Double.valueOf(maxTime));
        timeLimits.setMinTime(Double.valueOf(maxTime));
        setTimeLimits(timeLimits);
        setItemSessionControl(null);
        setAssessmentSectionCollection(assessmentSectionCollection);
        setEndPrompt(endPrompt);
    }

    /**
     * 部分（Part）构造函数(含时间限制,不含结束答题说明)
     *
     * @param identifier                  ID
     * @param prompt                      答题说明内容
     * @param navigationMode              导航模式
     * @param assessmentSectionCollection 大题集合
     * @param maxTime                     最大时间限制
     */
    public OpenTestPart(String identifier, OpenPrompt prompt, NavigationModeType navigationMode, List<OpenAssessmentSection> assessmentSectionCollection, int maxTime) {
        this(identifier, prompt, navigationMode, assessmentSectionCollection, maxTime, null);
    }

    /**
     * 部分（Part）构造函数
     *
     * @param imsTestPart IMS部分（Part）
     * @return the internal
     * @throws Exception the exception
     */
    public OpenTestPart(TestPartType imsTestPart) throws Exception {
        TranslateFromImsTestPart(imsTestPart);
    }

    /**
     * 从IMS部分（Part）转换
     *
     * @param imsTestPart IMS部分（Part）
     * @throws Exception the exception
     */
    private void TranslateFromImsTestPart(TestPartType imsTestPart) throws Exception {
        setIdentifier(imsTestPart.getIdentifier());
        setItemSessionControl(imsTestPart.getItemSessionControl());
        setTimeLimits(imsTestPart.getTimeLimits());
        if ((imsTestPart.getRubricBlock() != null) && (imsTestPart.getRubricBlock().get(0).getPrompt() != null) &&
                (imsTestPart.getRubricBlock().get(0).getPrompt().getContent().size() > 0)) {
            setPrompt(new OpenPrompt(imsTestPart.getRubricBlock().get(0).getPrompt().getContent().get(0).toString()));
        }
        if ((imsTestPart.getEndBlock() != null) && (imsTestPart.getEndBlock().getPrompt() != null) &&
                (imsTestPart.getEndBlock().getPrompt().getContent().size()  > 0)){
            setEndPrompt(new OpenPrompt(imsTestPart.getEndBlock().getPrompt().getContent().get(0).toString()));
        }
        setNavigationMode(imsTestPart.getNavigationMode());

        for (AssessmentSectionType section : imsTestPart.getAssessmentSection()) {
            getAssessmentSectionCollection().add(new OpenAssessmentSection(section));
        }
    }

    /**
     * 转换为IMS部分（Part）
     *
     * @return the test part type
     */
    public TestPartType TranslateToImsTestPart() {
        TestPartType imsTestPart = new TestPartType();
        imsTestPart.setIdentifier(getIdentifier());
        imsTestPart.setNavigationMode(getNavigationMode());
        imsTestPart.setSubmissionMode(SubmissionModeType.SIMULTANEOUS);
        RubricBlockType rubricBlock = new RubricBlockType();
        if (getPrompt() != null) {
            rubricBlock.setPrompt(getPrompt().ConvertToImsPrompt());
        }
        imsTestPart.getRubricBlock().add(rubricBlock);
        imsTestPart.getRubricBlock().get(0).getView().add(ViewType.CANDIDATE);
        imsTestPart.setTimeLimits(getTimeLimits());
        imsTestPart.setItemSessionControl(getItemSessionControl());
        if (getAssessmentSectionCollection() != null) {
            for (OpenAssessmentSection section : getAssessmentSectionCollection()) {
                imsTestPart.getAssessmentSection().add(section.TranslateToImsSection());
            }
        }
        EndBlock endBlock = new EndBlock();
        if (getEndPrompt() != null) {
            endBlock.setPrompt(getEndPrompt().ConvertToImsPrompt());
        }
        imsTestPart.setEndBlock(endBlock);
        return imsTestPart;
    }
}
